package com.yuorfei.html2pdf;

import com.itextpdf.text.pdf.BaseFont;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.*;

/**
 * html2pdf
 */
public class App {


    public static final String HTML_PATH = "test.html";
    public static final String PDF_PATH = "test.pdf";


    /**
     * 懒得写测试,就这么看吧.
     * @param args 参数
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        String pdfOutputPath = PDF_PATH;
        String htmlString = findHtmlString();
        byte[] dataArray = html2pdf(htmlString);
        if (dataArray != null && dataArray.length > 0) {
            // 输出pdf
            FileOutputStream fileOutputStream = new FileOutputStream(pdfOutputPath);
            fileOutputStream.write(dataArray);
            fileOutputStream.flush();
            fileOutputStream.close();
        }
        System.out.println("-----------ok------------");

    }


    /**
     * html转pdf
     * @param htmlString html字符
     * @return byte[]
     */
    public static byte[] html2pdf(String htmlString) {
        ByteOutputStream byteOutputStream = new ByteOutputStream();
        try {
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocumentFromString(htmlString);
            // 解决中文支持问题
            ITextFontResolver fontResolver = renderer.getFontResolver();
            fontResolver.addFont("simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            fontResolver.addFont("fontawesome-webfont.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //解决图片的相对路径问题,图片使用base64编码即可,否则就得如下使用,建议使用base64
//	        renderer.getSharedContext().setBaseURL("http://www.baidu.com/test.jpg");
            renderer.getSharedContext().setReplacedElementFactory(new B64ImgReplacedElementFactory());
            renderer.layout();
            renderer.createPDF(byteOutputStream);
            return byteOutputStream.getBytes();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                byteOutputStream.flush();
                byteOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 拿到html
     *
     * @return string
     */
    public static String findHtmlString() throws IOException {
        File file = new File(HTML_PATH);
        InputStream inputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuilder result = new StringBuilder();
        String temp;
        while ((temp = bufferedReader.readLine()) != null) {
            result.append(temp);
        }
        bufferedReader.close();
        inputStreamReader.close();
        inputStream.close();
        return result.toString();
    }
}
